<script>
    var setting = {
        view: {
            selectedMulti: false,
        },
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        edit: {
            enable: false
        }
    };

    $("#sub-add-user").click(function () {
        var username = $("#add-user").val();
        var passwd = $("#add-user-passwd").val();
        $.ajax({
            type:"post",
            url:"/ops/addsmbuser/",
            data:{"username":username,"passwd":passwd},
            dataType:"json",
            success:function (data) {
                $("#addsmbModal").modal("hide");
                spop({
                    template:data ,
                    style: 'success',
                    autoclose: 2000
                });
                setTimeout(location.reload(),5000);
            },
            error:function (data) {
                spop({
                    template: data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        })
    });


    $("td>a[name='view-perms']").click(function () {
        var cur_perms = $(this).next().text();
        var cur_user = $(this).next().attr("cur_user");
        $.ajax({
            type:"post",
            url:"/ops/getperms/",
            data:{"cur_perms":cur_perms},
            dataType:"json",
            success:function (data) {
                console.log(data);
                treeObj = $.fn.zTree.init($("#treeDemo"), setting, data);
                $("#smbModal").modal("show");
                $("#sub-smb-perms").attr("cur_user",cur_user)
            },
            error:function (data) {
                spop({
                    template: data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        });
    });

    $("#sub-smb-perms").click(function () {
        var cur_user = $(this).attr("cur_user");
        var perms_list = getPermsList();
        $.ajax({
            type: "post",
            url: "/ops/setperms/",
            data:{"cur_user":cur_user,"perms_list":perms_list},
            dataType: "json",
            success:function (data) {
                $("#smbModal").modal("hide");
                spop({
                    template:data ,
                    style: 'success',
                    autoclose: 2000
                });
                setTimeout(location.reload(),5000);
            },
            error:function (data) {
                spop({
                    template: data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        })
    });

    function getPermsList() {
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = treeObj.getCheckedNodes(true);
        var perms_list = "";
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].id >=1000){
                var perms = nodes[i].name;
                perms = perms.split("_")[0];
                perms_list += perms + ",";
            }
        }
        return perms_list
    }

    $("a[name='change-passwd']").click(function () {
        var cur_user = $(this).attr("cur_user");
        $("#passwdModal").modal("show");
        $("#sub-passwd-perms").attr("cur_user",cur_user)

    });

    $("#sub-passwd-perms").click(function () {
        var cur_user = $(this).attr("cur_user");
        var new_passwd = $("#new-passwd").val();
        $.ajax({
            type:"post",
            url:"/ops/setpasswd/",
            data:{"cur_user":cur_user,"new_passwd":new_passwd},
            dataType:"json",
            success:function (data) {
                $("#passwdModal").modal("hide");
                spop({
                    template:data ,
                    style: 'success',
                    autoclose: 2000
                });
                setTimeout(location.reload(),5000);
            },
            error:function (data) {
                spop({
                    template: data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        })
    });

    $("a[name='del-smb-user']").click(function () {
        var cur_user = $(this).attr("cur_user");
        $.ajax({
            type:"post",
            url:"/ops/deluser/",
            data:{"cur_user":cur_user},
            dataType:"json",
            success:function (data) {
                spop({
                    template:data ,
                    style: 'success',
                    autoclose: 2000
                });
                setTimeout(location.reload(),5000);
            },
            error:function (data) {
                spop({
                    template: data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        })
    });


</script>